4 * Class for handling database updates. Roughly based off of updaters.inc, with
5 * a few improvements :)
10 * Array of updates to perform on the database
14 protected $updates = array();
20 public function __construct( $db ) {
22 switch( $this->db
->getType() ) {
24 $this->updater
= new MysqlUpdater();
27 $this->updater
= new SqliteUpdater();
30 $this->updater
= new OracleUpdater();
33 throw new MWException( __METHOD__
. ' called for unsupported $wgDBtype' );
37 public function doUpdates() {
39 require_once( "$IP/maintenance/updaters.inc" );
41 foreach ( $this->updates
as $version => $updates ) {
42 foreach( $updates as $params ) {
43 $func = array_shift( $params );
44 call_user_func_array( $func, $params );
47 // some updates don't get recorded :(
48 if( $version !== 'always' ) {
49 $this->setAppliedUpdates( $version, $updates );
54 protected function loadUpdates() {
55 // If the updatelog table hasn't been upgraded, we can't use the new
56 // style of recording our steps. Run all to be safe
57 if( !$this->canUseNewUpdatelog() ) {
58 $this->updates
= $this->updater
->getUpdates();
60 foreach( $this->updater
->getUpdates() as $version => $updates ) {
61 $appliedUpdates = $this->getAppliedUpdates( $version );
62 if( !$appliedUpdates ||
$appliedUpdates != $updates ) {
63 $this->updates
[ $version ] = $updates;
67 $this->getOldGlobalUpdates();
70 protected function getAppliedUpdates( $version ) {
71 $key = "updatelist-$version";
72 $val = $this->db
->selectField( 'updatelog', 'ul_value',
73 array( 'ul_key' => $key ), __METHOD__
);
77 return unserialize( $val );
81 protected function setAppliedUpdates( $version, $updates = array() ) {
82 if( !$this->canUseNewUpdatelog() ) {
85 $key = "updatelist-$version";
86 $this->db
->delete( 'updatelog', array( 'ul_key' => $key ), __METHOD__
);
87 $this->db
->insert( 'updatelog',
88 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
93 * Updatelog was changed in 1.17 to have a ul_value column so we can record
94 * more information about what kind of updates we've done (that's what this
95 * class does). Pre-1.17 wikis won't have this column, and really old wikis
96 * might not even have updatelog at all
100 protected function canUseNewUpdatelog() {
101 return $this->db
->tableExists( 'updatelog' ) &&
102 $this->db
->fieldExists( 'updatelog', 'ul_value' );
106 * Before 1.17, we used to handle updates via stuff like $wgUpdates,
107 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
108 * of this in 1.17 but we want to remain back-compatible for awhile. So
109 * load up these old global-based things into our update list. We can't
110 * version these like we do with our core updates, so they have to go
113 private function getOldGlobalUpdates() {
114 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
115 $wgExtModifiedFields, $wgExtNewIndexes;
117 if( isset( $wgUpdates[ $this->db
->getType() ] ) ) {
118 foreach( $wgUpdates[ $this->db
->getType() ] as $upd ) {
119 $this->updates
['always'][] = $upd;
123 foreach ( $wgExtNewTables as $tableRecord ) {
124 $this->updates
['always'][] = array(
125 'add_table', $tableRecord[0], $tableRecord[1], true
129 foreach ( $wgExtNewFields as $fieldRecord ) {
130 if ( $fieldRecord[0] != 'user' ||
$doUser ) {
131 $this->updates
['always'][] = array(
132 'add_field', $fieldRecord[0], $fieldRecord[1],
133 $fieldRecord[2], true
138 foreach ( $wgExtNewIndexes as $fieldRecord ) {
139 $this->updates
['always'][] = array(
140 'add_index', $fieldRecord[0], $fieldRecord[1],
141 $fieldRecord[2], true
145 foreach ( $wgExtModifiedFields as $fieldRecord ) {
146 $this->updates
['always'][] = array(
147 'modify_field', $fieldRecord[0], $fieldRecord[1],
148 $fieldRecord[2], true